<template>
  <div class="modal-overlay" @click="handleOverlayClick">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3 class="modal-title">编辑用户信息</h3>
        <div class="header-accent"></div>
      </div>
      
      <div class="form-container">
        <div class="form-group">
          <label class="form-label">用户名</label>
          <input 
            type="text" 
            class="form-input disabled" 
            :value="user.username" 
            disabled
          />
        </div>

        <div class="form-group">
          <label class="form-label">新用户名</label>
          <input 
            type="text" 
            class="form-input" 
            v-model="newUsername"
            placeholder="请输入新用户名"
          />
        </div>
      </div>

      <div class="button-group">
        <button class="btn confirm-btn" @click="handleConfirm">
          <span class="btn-icon">✓</span>
          确认
        </button>
        <button class="btn reset-btn" @click="handleResetPassword">
          <span class="btn-icon">🔄</span>
          重置密码
        </button>
        <button class="btn delete-btn" @click="handleDelete">
          <span class="btn-icon">🗑️</span>
          删除
        </button>
        <button class="btn cancel-btn" @click="$emit('close')">
          <span class="btn-icon">✕</span>
          取消
        </button>
      </div>

      <!-- 删除确认弹窗 -->
      <div v-if="showDeleteConfirm" class="confirm-overlay">
        <div class="confirm-modal">
          <div class="confirm-icon">⚠️</div>
          <h4 class="confirm-title">确认删除</h4>
          <p class="confirm-text">确定要删除用户 "{{ user.username }}" 吗？此操作不可恢复。</p>
          <div class="confirm-buttons">
            <button class="btn confirm-delete-btn" @click="confirmDelete">确认删除</button>
            <button class="btn confirm-cancel-btn" @click="showDeleteConfirm = false">取消</button>
          </div>
        </div>
      </div>

      <!-- 重置密码确认弹窗 -->
      <div v-if="showResetConfirm" class="confirm-overlay">
        <div class="confirm-modal">
          <div class="confirm-icon">🔐</div>
          <h4 class="confirm-title">重置密码</h4>
          <p class="confirm-text">确定要重置用户 "{{ user.username }}" 的密码吗？</p>
          <div class="confirm-buttons">
            <button class="btn confirm-reset-btn" @click="confirmResetPassword">确认重置</button>
            <button class="btn confirm-cancel-btn" @click="showResetConfirm = false">取消</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { apiService } from '../utils/http'
import { tools } from '../utils/tools'

export default {
  name: 'UserEdit',
  props: {
    user: {
      type: Object,
      required: true
    }
  },
  emits: ['close', 'user-updated'],
  setup(props, { emit }) {
    const newUsername = ref('')
    const showDeleteConfirm = ref(false)
    const showResetConfirm = ref(false)

    const handleOverlayClick = () => {
      emit('close')
    }

    const handleConfirm = async () => {
      if (!newUsername.value.trim()) {
        alert('请输入新用户名')
        return
      }

      try {
        const headers = {token: `${tools.getTokenFromCookie()}`}
        await tools.authApiCall(
          () => apiService.post('/UserUpdate', {
            username: props.user.username,
            username_new: newUsername.value
          }, { headers }),
          useRouter(),
          useRoute()
        )
        emit('user-updated')
        emit('close')
      } catch (error) {
        console.error('更新用户失败:', error)
        alert('更新用户失败')
      }
    }

    const handleDelete = () => {
      showDeleteConfirm.value = true
    }

    const confirmDelete = async () => {
      try {
        const headers = {token: `${tools.getTokenFromCookie()}`}
        await tools.authApiCall(
          () => apiService.post('/UserDelete', { username: props.user.username }, { headers }),
          useRouter(),
          useRoute()
        )
        showDeleteConfirm.value = false
        emit('user-updated')
        emit('close')
      } catch (error) {
        console.error('删除用户失败:', error)
        alert('删除用户失败')
      }
    }

    const handleResetPassword = () => {
      showResetConfirm.value = true
    }

    const confirmResetPassword = async () => {
      try {
        const headers = {token: `${tools.getTokenFromCookie()}`}
        await tools.authApiCall(
          () => apiService.post('/PasswordReset', { username: props.user.username }, { headers }),
          useRouter(),
          useRoute()
        )
        showResetConfirm.value = false
        alert('密码重置成功')
        emit('close')
      } catch (error) {
        console.error('重置密码失败:', error)
        alert('重置密码失败')
      }
    }

    return {
      newUsername,
      showDeleteConfirm,
      showResetConfirm,
      handleOverlayClick,
      handleConfirm,
      handleDelete,
      confirmDelete,
      handleResetPassword,
      confirmResetPassword
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(74, 74, 106, 0.6);
  backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background: linear-gradient(135deg, #ffffff 0%, #f8f9ff 100%);
  border-radius: 24px;
  padding: 0;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(74, 74, 106, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.modal-header {
  background: linear-gradient(135deg, #7b68ee 0%, #9370db 100%);
  padding: 24px 24px 8px 24px;
  position: relative;
}

.modal-title {
  color: white;
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 16px 0;
  text-align: center;
}

.header-accent {
  height: 4px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  margin: 0 auto;
  width: 60px;
}

.form-container {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  color: #4a4a6a;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid #e8e8f7;
  border-radius: 12px;
  font-size: 16px;
  background: white;
  transition: all 0.3s ease;
  color: #4a4a6a;
}

.form-input:focus {
  outline: none;
  border-color: #7b68ee;
  box-shadow: 0 0 0 3px rgba(123, 104, 238, 0.1);
}

.form-input.disabled {
  background: #f8f9ff;
  color: #8a8aa3;
  border-color: #e8e8f7;
}

.button-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 0 24px 24px 24px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.confirm-btn {
  background: linear-gradient(135deg, #10b981 0%, #34d399 100%);
  color: white;
}

.reset-btn {
  background: linear-gradient(135deg, #f59e0b 0%, #fbbf24 100%);
  color: white;
}

.delete-btn {
  background: linear-gradient(135deg, #ef4444 0%, #f87171 100%);
  color: white;
}

.cancel-btn {
  background: rgba(138, 138, 163, 0.1);
  color: #6b7280;
  border: 2px solid #e5e7eb;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(74, 74, 106, 0.8);
  backdrop-filter: blur(4px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1001;
  padding: 20px;
}

.confirm-modal {
  background: white;
  border-radius: 20px;
  padding: 24px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(74, 74, 106, 0.3);
}

.confirm-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.confirm-title {
  color: #4a4a6a;
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 12px 0;
}

.confirm-text {
  color: #6b7280;
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 24px 0;
}

.confirm-buttons {
  display: flex;
  gap: 12px;
}

.confirm-delete-btn, .confirm-reset-btn, .confirm-cancel-btn {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.confirm-delete-btn {
  background: linear-gradient(135deg, #ef4444 0%, #f87171 100%);
  color: white;
}

.confirm-reset-btn {
  background: linear-gradient(135deg, #7b68ee 0%, #9370db 100%);
  color: white;
}

.confirm-cancel-btn {
  background: rgba(138, 138, 163, 0.1);
  color: #6b7280;
  border: 2px solid #e5e7eb;
}

@media (max-width: 480px) {
  .modal-content {
    border-radius: 20px;
    max-width: 100%;
  }
  
  .modal-header {
    padding: 20px 20px 8px 20px;
  }
  
  .form-container {
    padding: 20px;
  }
  
  .button-group {
    padding: 0 20px 20px 20px;
    grid-template-columns: 1fr;
  }
  
  .btn {
    padding: 14px 16px;
    font-size: 15px;
  }
}
</style>
